<template>
  <div class="goods-list">
    <div class="goods-item" v-for="item in goodslist" :key="item.id" @click="getDetail(item.id)">
      <img :src="item.img_url" alt="" />
      <h1 class="title">{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
        <span class="old">￥{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div>
    <mt-button @click="getMore" type="danger" size="large">加载更多</mt-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pageIndex: 1,
      goodslist:[]
    };
  },
  created(){
    this.getGoodsList()
    this.getMore()
  },
  methods: {
    getGoodsList() {
      this.$http.get('api/getgoods?pageindex=' + this.pageIndex).then(result=>{
        if(result.body.status===0) {
          this.goodslist = this.goodslist.concat(result.body.message)
        }
      })
    },
    getMore() {
      this.pageIndex++
      this.getGoodsList()
    },
    getDetail(id) {
      // 使用js的方式进行路由导航（也就是编程式导航）

      // 区分 this.$route 和 this.$router 两个对象
      // this.$route 是路由参数对象， 所有路由中的参数，如，params， query 都属于他
      // this.$router 是一个路由导航对象，用它可以方便的使用 JS 代码， 实现路由的前进后退，和跳转到URL地址

      //1. this.$router.push('/home/goodsInfo/' + id)
      //2. this.$router.push({path: '/home/goodsInfo/' + id})
      this.$router.push({ name: "goodsinfo", params: {id} })
    }
  }
};
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 8px;
  justify-content: space-between;

  .goods-item {
    width: 49%;
    border: 1px solid #cccccc;
    box-shadow: 0 0 8px #cccccc;
    margin: 4px 0;
    padding: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-height: 239px;

    img {
      width: 100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now{
          color: red;
          font-size: 16px;
          font-style: bold;
        }
        .old{
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
        font-size: 12px;
      }
    }
  }
}
</style>
